<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

     <!--   <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="表id">
          <a-input placeholder="请输入表id" v-decorator="['fdTableid', validatorRules.fdTableid ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="字段id">
          <a-input placeholder="请输入字段id" v-decorator="['fdFieldid', validatorRules.fdFieldid ]" />
        </a-form-item>-->


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="字段名">
          <a-input placeholder="请输入字段名" v-decorator="['fdFieldname', validatorRules.fdFieldname ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="描述">
          <a-input placeholder="请输入描述" v-decorator="['fdFielddesc', validatorRules.fdFielddesc ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="字段类型">
          <a-input placeholder="请输入字段类型" v-decorator="['fdFieldtype', validatorRules.fdFieldtype ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="字段长度">
          <a-input placeholder="请输入字段长度" v-decorator="['fdFieldlength', validatorRules.fdFieldlength ]" />
        </a-form-item>
        <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="字段精度">
        <a-input placeholder="请输入字段精度"  v-decorator="['fdFieldscale', validatorRules.fdFieldscale ]" />
      </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <j-dict-select-tag placeholder="请输入状态"  v-decorator="['fdStatus', {'initialValue':'0',rules: [{ required: true, message: '请输入状态!' }]}]" :triggerChange="true" dictCode="fieldinfo_state" />

        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则名称">
          <j-dict-select-tag placeholder="请输入规则名称"  v-decorator="['fdRulename',validatorRules.fdRulename]"  dictCode="dict_param,FDRULENAME,text"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则等级">
          <j-dict-select-tag placeholder="请输入规则等级"  v-decorator="['fdRulelevel',  {'initialValue':'1',rules: [{ required: true, message: '请输入规则等级!' }]}]"  dictCode="dict_param,RULELEVE,text"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "SysFieldinfoModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        fdTableid:{rules: [{ required: true, message: '请输入表id!' }]},
        fdFieldid:{rules: [{ required: true, message: '请输入字段id!' }]},
        fdFieldname:{rules: [{ required: true, message: '请输入字段名!' }]},
        fdFielddesc:{rules: [{ required: true, message: '请输入描述!' }]},
        fdFieldtype:{rules: [{ required: true, message: '请输入字段类型!' }]},
        fdFieldlength:{rules: [{ required: true, message: '请输入字段长度!' }]},
        fdFieldscale:{rules: [{ required: true, message: '请输入字段精度!' }]},
          fdStatus: {rules: [{ required: true, message: '请输入状态!' }]},
          fdRulename: {rules: [{ required: true, message: '请输入规则名称!' }]},
          fdRulelevel: {rules: [{ required: true, message: '请输入规则等级!' }]},
        },
        url: {
          add: "/meta/sysFieldinfo/add",
          edit: "/meta/sysFieldinfo/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'fdTableid','fdFieldid','fdFieldname','fdFielddesc','fdFieldtype','fdFieldlength','fdFieldscale','fdFieldorder','fdSrcfieldtype','fdHivefieldtype','fdStatus','fdRulename','fdRulelevel','fdTablesource','createUser','updateUser','isDetele'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>